﻿-- =============================================
-- Author:		Rui Miguel Santos
-- Create date: 2011-11-16
-- Description:	Retorna as datas dos dias da semana actual (com início na segunda-feira)
-- =============================================
CREATE FUNCTION ObtemDataDiasSemanaActual 
(
	@data DateTime
)
RETURNS 
@WeekDateDay TABLE
 (
 Segunda SMALLDATETIME,
 Terca SMALLDATETIME,
 Quarta SMALLDATETIME,
 Quinta SMALLDATETIME,
 Sexta SMALLDATETIME,
 Sabado SMALLDATETIME,
 Domingo SMALLDATETIME
 )
 
AS
BEGIN
DECLARE @dia INT
 SET @data = CAST(CONVERT(VARCHAR(10), @data, 101) AS SMALLDATETIME)
 SET @dia = DATEPART(dw, @data)
 INSERT INTO @WeekDateDay (Segunda, Terca, quarta, Quinta, Sexta, Sabado, Domingo)
 SELECT 
 DATEADD(dd, 2 - @dia, @data) Segunda,
 DATEADD(dd, 3 - @dia, @data) Terça,
 DATEADD(dd, 4 - @dia, @data) Quarta,
 DATEADD(dd, 5 - @dia, @data) Quinta,
 DATEADD(dd, 6 - @dia, @data) Sexta,
 DATEADD(dd, 7 - @dia, @data) Sabado,
 DATEADD(dd, 8 - @dia, @data) Domingo
 RETURN
 END
